import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/admin/Login";
// import Home from '../views/Home.vue'
import Index from "../views/Index";
import AdminLayout from "../views/admin/AdminLayout";
import Route from "../views/Route";
import Detail from "../views/Detail";
import PurchaseHotel from "../views/PurchaseHotel";
import PurchaseTicket from "../views/PurchaseTicket";
import Story from "../views/Story";
import About from "../views/About"
import Hotel from "../views/Hotel";
import HotelDetail from "../views/HotelDetail";
import Contact from "../views/Contact";
import Register from "../views/admin/Register";
import Pay from "../views/Pay";
import Success from "../views/Success";
import User from "../components/admin/User";
import Category from "../components/admin/Category";
import Order from "../components/admin/Order";
import Product from "../components/admin/Product";
import Write from "../components/admin/Write";
import Edit from "../components/admin/Edit";
import Story1 from '../components/admin/Story'
import WriteStory from "../components/admin/WriteStory";
import EditStory from "../components/admin/EditStory";
import Role from "../components/admin/Role";
import Permission from "../components/admin/Permission";
import {Message} from "element-ui";
import Test from "../views/Test";
import NotFound from "../views/NotFound";
import StoryDetail from "../views/StoryDetail";
import Logs from "../components/admin/Logs";
import AdminIndex from "../components/admin/AdminIndex";
Vue.use(VueRouter)

const routes = [
    {
        path: '/test',
        name: 'Test',
        component: Test
    },
    {
        path: '/',
        name: 'Index',
        component: Index
    },
    {
        path: '/success',
        name: 'Success',
        component: Success
    },
    {
        path: '/pay',
        name: 'Pay',
        component: Pay
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },

    {
        path: '/contact',
        name: 'Contact',
        component: Contact
    },
    {
        path: '/hoteldetail',
        name: 'HotelDetail',
        component: HotelDetail
    },
    {
        path: '/hotel',
        name: 'Hotel',
        component: Hotel
    },
    {
        path: '/about',
        name: 'About',
        component: About
    },
    {
        path: '/story',
        name: 'Story',
        component: Story
    },
    {
        path: '/storydetail',
        name: 'StoryDetail',
        component: StoryDetail
    },
    {
        path: '/ticket',
        name: 'PurchaseTicket',
        component: PurchaseTicket
    },
    {
        path: '/buyhotel',
        name: 'PurchaseHotel',
        component: PurchaseHotel
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/about',
        name: 'About',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/route',
        name: 'Route',
        component: Route
    },
    {
        path: '/detail',
        name: 'Detail',
        component: Detail
    },
//    后台路由开始
    {
        path: '/admin',
        name: 'AdminLayout',   // 头部、左侧框架
        component: AdminLayout,
        meta: {
            requireAuth: true
        },
        children: [
            {
                path: '/log',
                name: 'Logs',
                component: Logs,
                meta: {
                    requireAuth: true
                },

            },
            {
                path: '/adminindex',
                name: 'AdminIndex',
                component: AdminIndex,
                meta: {
                    requireAuth: true
                },

            },
            // {
            //     path: '/user',
            //     name: 'User',
            //     component: User,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            // {
            //     path: '/category',
            //     name: 'Category',
            //     component: Category,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            // {
            //     path: '/order',
            //     name: 'Order',
            //     component: Order,
            //     meta: {
            //         requireAuth: true
            //     },
            // },
            // {
            //     path: '/product',
            //     name: 'Product',
            //     component: Product,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            // {
            //     path: '/write',
            //     name: 'Write',
            //     component: Write,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            // {
            //     path: '/edit',
            //     name: 'Edit',
            //     component: Edit,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            // {
            //     path: '/adminstory',
            //     name: 'AdminStory',
            //     component: Story1,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            // {
            //     path: '/writestory',
            //     name: 'WriteStory',
            //     component: WriteStory,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            // {
            //     path: '/editstory',
            //     name: 'EditStory',
            //     component: EditStory,
            //     meta: {
            //         requireAuth: true
            //     },
            //
            // },
            {
                path: '/role',
                name: 'Role',
                component: Role,
                meta: {
                    requireAuth: true
                },

            },
            // {
            //     path: '/permission',
            //     name: 'Permission',
            //     component: Permission,
            //     meta: {
            //         requireAuth: true
            //     },
            // },
        ]

    },
    {
        path: '*',
        name: 'NotFound',
        component: NotFound
    }
]
let map = {
    '权限管理': {
        path: '/permission',
        name: 'Permission',
        component: Permission,
        meta: {
            requireAuth: true
        },
    },
    '角色管理': {
        path: '/role',
        name: 'Role',
        component: Role,
        meta: {
            requireAuth: true
        },

    },
    '商品详情编辑': {
        path: '/edit',
        name: 'Edit',
        component: Edit,
        meta: {
            requireAuth: true
        },

    },
    '商品详情添加': {
        path: '/write',
        name: 'Write',
        component: Write,
        meta: {
            requireAuth: true
        },

    },
    '编辑故事': {
        path: '/editstory',
        name: 'EditStory',
        component: EditStory,
        meta: {
            requireAuth: true
        },

    }
    , '写故事': {
        path: '/writestory',
        name: 'WriteStory',
        component: WriteStory,
        meta: {
            requireAuth: true
        },

    },
    '订单管理': {
        path: '/order',
        name: 'Order',
        component: Order,
        meta: {
            requireAuth: true
        },
    }
    ,
    '用户管理': {
        path: '/user',
        name: 'User',
        component: User,
        meta: {
            requireAuth: true
        },

    },
    '商品管理': {
        path: '/product',
        name: 'Product',
        component: Product,
        meta: {
            requireAuth: true
        },

    },
    '分类管理': {
        path: '/category',
        name: 'Category',
        component: Category,
        meta: {
            requireAuth: true
        },

    },
    '故事管理': {
        path: '/adminstory',
        name: 'AdminStory',
        component: Story1,
        meta: {
            requireAuth: true
        },

    },
}

function local() {
    let token = localStorage.getItem('token')
    // console.log(token)
    // console.log(routes)
    let permissionList
        = JSON.parse(localStorage.getItem('permissionList'))
    // console.log(permissionList)
    // console.log(map.用户管理);
    for (let route of routes) {
        if (route.path == '/admin') {
            for (let i = 0; i < permissionList.length; i++) {
                let permissionName = permissionList[i].permissionName
                // console.log(permissionName)
                if (permissionName == '用户管理') {
                    route.children.push(map.用户管理)
                } else if (permissionName == '商品管理') {
                    route.children.push(map.商品管理)
                    route.children.push(map.商品详情添加)
                    route.children.push(map.商品详情编辑)
                } else if (permissionName == '分类管理') {
                    route.children.push(map.分类管理)
                } else if (permissionName == '故事管理') {
                    route.children.push(map.故事管理)
                    route.children.push(map.写故事)
                    route.children.push(map.编辑故事)

                } else if (permissionName == '订单管理') {
                    route.children.push(map.订单管理)
                } else if (permissionName == '权限管理') {
                    route.children.push(map.权限管理)
                } else if (permissionName == '角色管理') {
                    route.children.push(map.角色管理)
                }

            }
            // console.log(route.path)
            // console.log(route.children)
        }
    }
    // console.log(routes)

    router.addRoutes(routes)

}

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    // console.log(to.matched[0].path=='/admin')
    if (to.meta.requireAuth) {  // 判断路由是否需要权限
        let token = localStorage.getItem('token')
        if (token) {
            next()
        } else {
            Message.error('请先登录')
            next({
                path: '/login',
                query: {redirect: to.path}
            })
        }
    }
    next()
})
router.afterEach((to, from, next) => {
    local()

    window.scrollTo(0, 0);
})
export default router
